using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DiskSpace
{
    public partial class Tray : Form
    {
        private Icon m_trayIcon = Properties.Resources.TrayIcon;
        private Settings m_settingsForm = new Settings();
        private Info m_infoForm;

        public Tray()
        {
            //constructor for the form
            InitializeComponent();
            InitializeNotifyIcon();
        }

        private void Tray_Load(object sender, EventArgs e)
        {
            // Supply the interface to the setting on to the Info form
            // at this stage it is the settings form that supplies this interface
            m_infoForm = new Info(m_settingsForm);
            m_infoForm.Hide();

            this.Hide();
        }

        private void InitializeNotifyIcon()
        {
            //setup the Icon
            //NotifyIcon TrayIcon = new NotifyIcon();
            trayIcon.Icon = m_trayIcon;
            trayIcon.Text = "Click to view disk usage";
            trayIcon.Visible = true;

            //add the menu items to the context menu of the NotifyIcon
            ContextMenu notifyIconMenu = new ContextMenu();

            MenuItem openMenuItem = new MenuItem("Open", OpenHandler);
            openMenuItem.DefaultItem = true;
            notifyIconMenu.MenuItems.Add(openMenuItem);

            notifyIconMenu.MenuItems.Add("-");

            // Pass the context menu on so that the settings options can be added
            m_settingsForm.PopulateMenu(notifyIconMenu);

            notifyIconMenu.MenuItems.Add("-");

            notifyIconMenu.MenuItems.Add(new MenuItem("Exit", ExitAppHandler));

            trayIcon.ContextMenu = notifyIconMenu;
            this.Hide();
        }

        /// <summary>
        /// Handle Open option being selected from Context menu and display form if not already visible.
        /// </summary>
        /// <param name="sender">This will always be the menu item</param>
        /// <param name="e">Event arguments not used in this case</param>
        private void OpenHandler(object sender, EventArgs e)
        {
            if (m_infoForm.Visible == false)
            {
                m_infoForm.Show();
            }
        }

        private void trayIcon_Click(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (m_infoForm.Visible == true)
                {
                    m_infoForm.Hide();
                }
                else
                {
                    m_infoForm.Show();
                }
            }
        }

        public void ExitAppHandler(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}